Lab 22
Structure Arrays
CS211 Lab Policy:
- This lab exercise will not be graded.
- Submit as much as you have completed before the end of the lab period in
which it is assigned.
- If you do not finish this lab work, it is to your advantage to finish it
outside of class. Please re-submit your finished work to the course web
site.
- You may receive help from anyone in completing this lab.
- You may not submit another student's code as part of your
lab.
Instructions:
You will create one MATLAB program named Lab22()
in file Lab22.m for this lab.
The instructions below ask you to perform the same tasks as the previous lab
assignment, Lab 21, but this time you will use data that is stored in a
structure array.
Open your Lab21.m file and use the "Save as..." command to save it into a new
file called Lab22.m. Modify this new file
for this assignment.
Modify the name of your function to Lab22().
Then select all of the statements in the body of the
Lab22()function and use the Text
menu, Comment command, to comment out all of the statements. You will
then un-comment and modify sections of the code as described below.
- Clear the command window.
- Use the MATLAB statement below to read the Marathon race data from a file into
a set of arrays. (Replace the 3 MATLAB statements that read the data file
with this single statement.)
[Overall_position Age_position Age_total Name
Age Gender Town State Ascent Descent Total] ...
= textread('PikesPeakMarathon2007.txt', '%d %d%*c%d %24c %d %c %15c
%2c %7c %7c %7c');
- Use the whos command to
display information about the arrays that currently store the data.
- Use a struct() function call to
create a structure array called Runner
that defines a field for each of the data arrays created by the
textread()
function. You should define 11 fields. Make the size of the structure array
be equal to the length of one of the arrays. (Why does it not matter which
array you use?) Set every field value for every element to
[] (an empty vector).
- Re-organize the "parallel arrays" into a single structure array using a
loop similar to the one shown below. Fill in appropriate statements for the
fields not shown using the field names you created in step 4. Use the
strtrim() function on all string data
to remove their leading and trailing blanks. This can save significant
amounts of memory.
for J = 1 : length(overall_position)
Runner(J).finish_position =
Overall_position(J);
Runner(J).age_finish
= Age_position(J);
..
Runner(J).name
= strtrim( Name(J,:) );
..
end
- Use the whos Runner command to
display information about your structure arrays. Does the displayed
information make sense to you? If you have any questions, please ask your
instructor.
- Before you begin plotting, make sure you can get specific data
values out of the structure array. Using fprintf(),
display the following values:
- The name of the 20th runner.
- The home town and state of the last runner.
- The age of the first runner.
- The first letter in the 5th runner's name.
- Now it's time to plot! Un-comment a group of statements that performs a
plot, modify its syntax to correctly use the structure array, and then
verify that it creates a correct graph. After this section of code is
working properly, move on to the next plotting task.
Turn-in:
Submit your
Lab22.m file.